//
//  RJTabBar.m
//  AirQuality
//
//  Created by Po on 15/3/10.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJTabBarView.h"

@interface RJTabBarView()

@property (retain, nonatomic) UIImageView * tabbarImageView;        //标签栏背景
@property (retain, nonatomic) UIButton * currentButton;             //当前选中按钮
@property (retain, nonatomic) UIView * messageStatusView;           //消息红点

@property (retain, nonatomic) NSMutableArray * buttons;             //所有按钮
@property (assign, nonatomic) NSUInteger tabbarItemWidth;           //Item宽度
@property (assign, nonatomic) BOOL isAnim;                          //动画中


- (void)initData;
- (void)initInterface;
@end

@implementation RJTabBarView

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initData];
        [self initInterface];
    }
    return self;
}

#pragma mark - user-defined initialization function
- (void)initData
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectPao) name:@"selectPao" object:nil];
    _currentPage = 1;
    [RJUserData defaultRJUserData].isTabbarShow = YES;
    _isAnim = NO;
    _buttons = [[NSMutableArray alloc] init];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSelf) name:@"showTabbar" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideSelf) name:@"hideTabbar" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setMessageDataStatus:) name:@"setMessageDataStatus" object:nil];
}

- (void)initInterface
{
    [self setBackgroundColor:GetRGBColor(240, 246, 246, 1)];
    [self getAllButton];
    
    CAShapeLayer * layer = [CAShapeLayer layer];
    [layer setFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    [layer setBackgroundColor:SysMainColor.CGColor];
    [self.layer addSublayer:layer];
}

- (void)getAllButton
{
     NSArray * tabbarImageNameArray = @[@"home",@"city",@"write",@"remind",@"Setting"];
    _tabbarItemWidth = ScreenWidth / tabbarImageNameArray.count;
    for (NSUInteger i = 0; i < tabbarImageNameArray.count ; i ++) {
        NSString * imageName = [tabbarImageNameArray[i] stringByAppendingString:@".png"];
        UIButton * button = [self getButtonWith:[UIImage imageNamed:imageName] frame:CGRectMake(_tabbarItemWidth * i, 0, _tabbarItemWidth, sysTabbarHeight)];
        [button setTag:101 + i ];
        NSString * selectImage = [NSString stringWithFormat:@"%@_Select.png",tabbarImageNameArray[i]];
        [button setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
        CGFloat type = DeviceType;
        [button setImageEdgeInsets:UIEdgeInsetsMake(
                                                    6,
                                                    (type > 6 ? 30: type > 5.5 ? 26 : 21),
                                                    6,
                                                    (type > 6 ? 30: type > 5.5 ? 26 : 21)
                                                    )];
        [button setSelected:NO];
        [self addSubview:button];
        if (i == 0) {
            _currentButton = button;
            [button setSelected:YES];
            [button setBackgroundColor:SysMainColor];
        }
        [_buttons addObject:button];
    }
}

- (UIButton *)getButtonWith:(UIImage *)image frame:(CGRect)rect
{
    UIButton * button = [[UIButton alloc] initWithFrame:rect];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (UIView *)getMessageStatusView
{
    if (!_messageStatusView) {
        _messageStatusView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth - 20, 5, 10, 10)];
        [_messageStatusView setBackgroundColor:[UIColor redColor]];
        [_messageStatusView.layer setCornerRadius:5];
    }
    return _messageStatusView;
}

#pragma mark - function

- (void)nextPage{
    for (UIButton * button in _buttons) {
            button.userInteractionEnabled = YES;
    }
}
#pragma mark - event
- (void)pressButton:(UIButton *)sender
{
    NSUInteger pressPageNum = sender.tag - 100;
    if (pressPageNum == _currentPage) {
        return;
    }
    [RJUserData defaultRJUserData].paoPaoName = @"";
    [_currentButton setSelected:NO];
    [_currentButton setBackgroundColor:[UIColor clearColor]];
    sender.selected = YES;
    _currentButton = sender;
    [_currentButton setBackgroundColor:SysMainColor];
    
    if (_delegate) {
        [_delegate RJTabBarDidSelectedPage:(sender.tag - 100) lastPageNum:_currentPage];
    }

    //更新页面计数
    _currentPage = sender.tag - 100;
}

- (void)hideSelf
{
    if (_isAnim) {
        return;
    }
    [RJUserData defaultRJUserData].isTabbarShow = NO;
    _isAnim = YES;
    [UIView animateWithDuration:0.3f animations:^{
        CGRect rect = self.frame;
        rect.origin.y += rect.size.height;
        [self setFrame:rect];
    } completion:^(BOOL finished) {
        if (finished) {
            _isAnim = NO;
        }
    }];
}

- (void)showSelf
{
    if (_isAnim) {
        return;
    }
    [RJUserData defaultRJUserData].isTabbarShow = YES;
    _isAnim = YES;
    [UIView animateWithDuration:0.2f animations:^{
        CGRect rect = self.frame;
        rect.origin.y = ScreenHeight - rect.size.height;
        [self setFrame:rect];
    } completion:^(BOOL finished) {
        if (finished) {
            _isAnim = NO;
        }
    }];
}

- (void)setMessageDataStatus:(NSNotification *)notification
{
    NSDictionary * data = notification.userInfo;
    if (data[@"haveData"]) {
        [self addSubview:[self getMessageStatusView]];
    } else {
        if (_messageStatusView) {
            [_messageStatusView removeFromSuperview];
            _messageStatusView = nil;
        }
    }
}

- (void)reSetSelectCount:(NSUInteger)count
{
    if(_currentPage != count)
    {
        UIButton * button = _buttons[count - 1];
        _currentPage = count;
        [_currentButton setSelected:NO];
        [_currentButton setBackgroundColor:[UIColor clearColor]];
        button.selected = YES;
        _currentButton = button;
        [_currentButton setBackgroundColor:SysMainColor];
    }
}

- (void)selectPao
{
    UIButton * button = _buttons[1];
    _currentPage = 2;
    [_currentButton setSelected:NO];
    [_currentButton setBackgroundColor:[UIColor clearColor]];
    button.selected = YES;
    _currentButton = button;
    [_currentButton setBackgroundColor:SysMainColor];
}

@end
